﻿@namespace BootstrapBlazor.Components
@typeparam TItem

<div class="float-left table-toolbar-button">
    <CascadingValue Value="this" IsFixed="true">
        @ChildContent
    </CascadingValue>
    <RenderTemplate>
        @if (Buttons.Count > 0)
        {
            <div class="btn-toolbar btn-group d-none d-sm-inline-flex">
                @foreach (var button in Buttons)
                {
                    @if (button is TableToolbarButton<TItem> b)
                    {
                        <Button AdditionalAttributes="b.AdditionalAttributes"
                                Color="@b.Color" Icon="@b.Icon" Text="@b.Text" IsAsync="@b.IsAsync"
                                OnClickWithoutRender="@(() => OnToolbarButtonClick(b))"
                                IsDisabled="@GetDisabled(b)"></Button>
                    }
                    else if (button is TableToolbarPopconfirmButton<TItem> pb)
                    {
                        <PopConfirmButton AdditionalAttributes="pb.AdditionalAttributes"
                                          Color="@pb.Color" Icon="@pb.Icon" Text="@pb.Text"
                                          IsDisabled="@pb.IsDisabled" IsBlock="@pb.IsBlock" IsOutline="@pb.IsOutline"
                                          OnBeforeClick="@pb.OnBeforeClick" OnClose="@pb.OnClose" OnConfirm="@pb.OnConfirm"
                                          ConfirmIcon="@pb.ConfirmIcon" ConfirmButtonColor="@pb.ConfirmButtonColor"
                                          ConfirmButtonText="@pb.ConfirmButtonText" CloseButtonColor="@pb.CloseButtonColor"
                                          CloseButtonText="@pb.CloseButtonText" Content="@pb.Content">
                        </PopConfirmButton>
                    }
                }
            </div>
            <div class="btn-gear btn-group d-sm-none">
                <button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" type="button">
                    <i class="fa fa-fw fa-gear"></i>
                </button>
                <div class="dropdown-menu">
                    @foreach (var button in Buttons)
                    {
                        @if (button is TableToolbarButton<TItem> b)
                        {
                            <div class="dropdown-item" @onclick="@(e => OnToolbarButtonClick(b))">
                                <i class="@b.Icon"></i>
                            </div>
                        }
                        else if (button is TableToolbarPopconfirmButton<TItem> pb)
                        {
                            <div class="dropdown-item" @onclick="@pb.OnConfirm">
                                <i class="@pb.Icon"></i>
                            </div>
                        }
                    }
                </div>
            </div>
        }
    </RenderTemplate>
</div>
